//
//  PasswordViewController.m
//  TTPassword
//
//  Created by ttcloud on 16/6/20.
//  Copyright © 2016年 ttcloud. All rights reserved.
//

#import "MoneyPasswordViewController.h"
#import "TTConst.h"
#import "CContactMgr.h"
#import "CContactModel.h"
#import "MBProgressHUD.h"
#import "CXmppRequestMgr.h"
@interface MoneyPasswordViewController ()

@end

@implementation MoneyPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.navigationItem.title =@"安全码设置";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    [self addsubview];
    // Do any additional setup after loading the view.
}
-(void)addsubview
{
    
    
    self.label=[[UILabel alloc]initWithFrame:CGRectMake(0, 163.5, ScreenWidth, 16)];
    self.label.text=@"请重新设着您的密码，以保障您的资金安全";
    self.label.textColor=UIColorFromRGB(0x3e3e3e);
    self.label.font=[UIFont systemFontOfSize:13];
    self.label.textAlignment=NSTextAlignmentCenter;
    [self.view addSubview:self.label];
    
    self.secondtip=[[UILabel alloc]initWithFrame:CGRectMake(0,246, ScreenWidth, 16)];
    self.secondtip.text=@"请再次输入您的安全码";
    self.secondtip.textColor=UIColorFromRGB(0xff3674);
    self.secondtip.font=[UIFont systemFontOfSize:13];
    self.secondtip.textAlignment=NSTextAlignmentCenter;
    self.secondtip.hidden=YES;
    [self.view addSubview:self.secondtip];
    
    
    self.password = [[TTPasswordView alloc] initWithFrame:CGRectMake((ScreenWidth-256.5)/2, 193.5, 256.5, 39)];
    self.password.elementCount = 6;
    self.password.elementColor=UIColorFromRGB(0xd5d5d5);
    [self.view addSubview:self.password];
    __block MoneyPasswordViewController *weakself=self;
    self.password.passwordBlock = ^(NSString *password) {
        if (password.length==6) {
            [weakself enterCode:password];
        }
        
    };
    
    
}
-(void)enterCode:(NSString *)code
{
    
    if (!self.firstCode) {
        self.firstCode=code;
        self.secondtip.text=@"请再次输入您的密码";
        self.secondtip.hidden=NO;
        [self.password clearText];
        [self.password.textField becomeFirstResponder];
    }
    else if (self.firstCode&&[self.firstCode isEqualToString:code])
    {
        __weak typeof(self) weakSelf = self;
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.labelText = @"设置中...";
        CContactModel *currentUser = [[[CContactMgr alloc]init] loadCurContact];
        [[CXmppRequestMgr shareInstance] changePassWordWithPassword:code AnduserId:currentUser.UserID successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
            if ([responseObject[@"result"] isEqualToString:@"success"]) {
                [hud hide:YES];
                dispatch_async(dispatch_get_main_queue(), ^{
                    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"密码修改成功" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                        [weakSelf.navigationController popViewControllerAnimated:YES];
                    }];
                    [alertController addAction:cancelAction];
                    [self presentViewController:alertController animated:YES completion:nil];

                });
            }
            else
            {
                [hud hide:YES];
                [MBProgressHUD showError:@"修改失败,请重试"];
                [weakSelf.password clearText];
                [weakSelf.password.textField becomeFirstResponder];
                weakSelf.firstCode=nil;
            }
        } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
            [hud hide:YES];
            [MBProgressHUD showError:@"修改失败,请重试"];
            [weakSelf.password clearText];
            [weakSelf.password.textField becomeFirstResponder];
            weakSelf.firstCode=nil;

        }];

    }
    else
    {
        self.secondtip.text=@"您两次输入的安全码不匹配，请重新设置";
        [self.password clearText];
        [self.password.textField becomeFirstResponder];
        self.firstCode=nil;
    }
    
}

-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
